<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all"/>
    <script src="/layuiadmin/layui/layui.js"></script>
</head>
<body style="background-color:#fff">

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="" class="layui-form layui-form-pane" lay-filter="layui-form-pane" id="sbumitForm">
            <div class="layui-row layui-col-space10 layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">活动名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" value="" class="layui-input" lay-verify="required" autocomplete="off">
                    </div>
                </div>
            </div>

            <div class="layui-row layui-col-space10 layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">活动介绍</label>
                    <div class="layui-input-block">
                        <input type="text" name="introduce" value="" class="layui-input" lay-verify="required" autocomplete="off">
                    </div>
                </div>
            </div>

            <div class="layui-row layui-col-space10 layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">活动规则</label>
                    <div class="layui-input-block">
                        <textarea name="rule_desc" value="" style="width: 100%;height: 100px;resize: none;" autocomplete="off"></textarea>
                    </div>
                </div>
            </div>

            <div class="layui-row layui-col-space10 layui-form-item">
                <label class="layui-form-label">PC图标</label>
                <div class="layui-input-inline">
                    <input name="pc_bg_pic" lay-verify="required" id="pc_bg_pic" placeholder="请上传图片或输入地址" value="" class="layui-input" autocomplete="off">
                </div>
                <div class="layui-input-inline layui-btn-container" style="width: auto;">
                    <button type="button" class="layui-btn layui-btn-primary" id="Upload_pc_bg_pic">
                        <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                    <span class="layui-btn layui-btn-primary" onclick="icon_view('pc_bg_pic')">查看图片</span >
                </div>
            </div>

            <div class="layui-row layui-col-space10 layui-form-item">
                <label class="layui-form-label">APP图标</label>
                <div class="layui-input-inline">
                    <input name="app_bg_pic" lay-verify="required" id="app_bg_pic" autocomplete="off"  placeholder="请上传图片或输入地址" value="" class="layui-input" >
                </div>
                <div class="layui-input-inline layui-btn-container" style="width: auto;">
                    <button type="button" class="layui-btn layui-btn-primary" id="Upload_app_bg_pic">
                        <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                    <span class="layui-btn layui-btn-primary" onclick="icon_view('app_bg_pic')">查看图片</span >
                </div>
            </div>

            <div class="layui-row layui-col-space10 layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">参与用户</label>
                    <div class="layui-input-block">
                        <input type="radio" value="0" name="join_user_type" lay-verify="required" title="全部" checked />
                        <input type="radio" value="1" name="join_user_type" lay-verify="required" title="贵族" />
                        <input type="radio" value="2" name="join_user_type" lay-verify="required" title="普通" />
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">抽奖消耗:</label>
                <div class="layui-input-block">
                    <table class="layui-table layui-form" style="table-layout:fixed;width: 100%">
                        <tr>
                            <td style="text-align:right;font-size: 16px;border-color:#fff;background:#fff;height: 30px;width: 15%;padding:0px 10px 0px 0px" >宝石抽奖</td>
                            <td style="text-align:center;font-size: 12px;border-color:#fff;background:#fff;height: 30px;width: 60%;padding:0px">
                                <input type="text" name="diamond_cost" value="" class="layui-input" lay-verify="required" autocomplete="off"; style="padding: 0px" onkeyup="input_num(this)">
                            </td>
                            <td style="text-align:left;border-color:#fff;background:#fff;height: 30px;width: 10%;padding:0px 0px 0px 5px">
                                次
                            </td>
                        </tr>

                        <tr>
                            <td style="border-color:#fff;background:#fff;"></td>
                            <td style="border-color:#fff;background:#fff;"></td>
                            <td style="border-color:#fff;background:#fff;"></td>
                        </tr>

                        <tr>
                            <td style="text-align:right;font-size: 16px;border-color:#fff;background:#fff;height: 30px;width: 15%;padding:0px 10px 0px 0px" >鹰钻抽奖</td>
                            <td style="text-align:center;font-size: 12px;border-color:#fff;background:#fff;height: 30px;width: 60%;padding:0px">
                                <input type="text" name="gold_cost" value="" class="layui-input" lay-verify="required" autocomplete="off"; style="padding: 0px" onkeyup="input_num(this)">
                            </td>
                            <td style="text-align:left;border-color:#fff;background:#fff;height: 30px;width: 10%;padding:0px 0px 0px 5px">
                                次
                            </td>
                        </tr>
                    </table>
                </div>
            </div>

            <div class="layui-row layui-col-space10 layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">开始时间</label>
                    <div class="layui-input-block">
                        <input type="text" name="act_st_time" class="layui-input" id="act_st_time" readonly placeholder="开始日期" autocomplete="off">
                    </div>
                </div>
            </div>

            <div class="layui-row layui-col-space10 layui-form-item">
                <div class="layui-col-lg6">
                    <label class="layui-form-label">结束时间</label>
                    <div class="layui-input-block" id="act_ed_time_div">
                        <input type="text" name="act_ed_time" class="layui-input" id="act_ed_time" readonly placeholder="结束日期" autocomplete="off">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <span class="layui-btn"  lay-submit lay-filter="formSubmit" style="width: 100%">确认添加</span>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="/assets/cos-js-sdk-v5/cos-js-sdk-v5.min.js"></script>
<script>

    layui.use(['form', 'upload', 'laydate'], function () {
        var $ = layui.jquery, form = layui.form, layer = layui.layer, upload = layui.upload;
        var laydate = layui.laydate;
        $(this).removeAttr("lay-key");
        laydate.render({
            elem: '#act_st_time', //指定元素
            min: '<?php echo date("Y-m-d H:i:s");?>',
            type: 'datetime',
            trigger: 'click',
            done: function (value, date, endDate) { //控件选择完毕后的回调---点击日期、清空、现在、确定均会触发。
                $("#act_ed_time_div").empty();
                $("#act_ed_time_div").append('<input type="text" type="text" name="act_ed_time" class="layui-input"  id="act_ed_time" readonly  placeholder="结束日期" autocomplete="off" />');
                laydate.render({
                    elem: '#act_ed_time', //指定元素
                    type: 'datetime',
                    min: value,
                    trigger: 'click',
                });
            },
        });
        laydate.render({
            elem: '#act_ed_time', //指定元素
            type: 'datetime',
            min: '<?php echo date("Y-m-d H:i:s");?>',
            trigger: 'click',
        });
        upload.render({
            elem: '#Upload_pc_bg_pic',
            auto: false,
            accept: 'images',
            exts: 'svg|png|jpg|jpeg|gif|webpg',
            size: 1024 * 1024,
            choose: function (obj) {
                layer.load();
                var choosefile;
                obj.preview(function (index, file, result) {
                    choosefile = file;
                });

                $.ajax({
                    type: "POST", //请求方式
                    url: '/admin/rotary_table/signForCos', //请求地址
                    dataType: "json", //返回的数据类型
                    //请求成功
                    success: function (data) {
                        if (data.code == 0) {
                            var cos = new COS({
                                getAuthorization: function (options, callback) {
                                    var credentials = data.data.credentials;
                                    callback({
                                        TmpSecretId: credentials.tmpSecretId,
                                        TmpSecretKey: credentials.tmpSecretKey,
                                        XCosSecurityToken: credentials.sessionToken,
                                        ExpiredTime: data.data.expiredTime
                                    });
                                }
                            });
                            cos.putObject({
                                Bucket: data.data.bucket,
                                Region: data.data.region,
                                Key: 'images/' + data.data.filename + choosefile.name,
                                Body: choosefile,
                            }, function (err, res) {
                                layer.closeAll('loading');
                                if (err) {
                                    layer.msg("上传失败", {icon: 5});
                                } else {
                                    if (res.statusCode == 200) {
                                        layer.msg('上传成功', {icon: 1, time: 2000});
                                        $("#pc_bg_pic").val("https://" + res.Location);
                                    } else {
                                        layer.msg("图片上传失败", {icon: 5});
                                    }
                                }
                            });
                        } else {
                            layer.closeAll('loading');
                            layer.msg(data.msg, {icon: 5});
                        }
                    },
                    //请求失败
                    error: function (e) {
                        layer.closeAll('loading');
                        layer.msg('上传出现异常,请稍后再试', {icon: 2});
                    }
                });
            }
        });

        upload.render({
            elem: '#Upload_app_bg_pic',
            auto: false,
            accept: 'images',
            exts: 'svg|png|jpg|jpeg|gif|webpg',
            size: 1024 * 1024,
            choose: function (obj) {
                layer.load();
                var choosefile;
                obj.preview(function (index, file, result) {
                    choosefile = file;
                });

                $.ajax({
                    type: "POST", //请求方式
                    url: '/admin/rotary_table/signForCos', //请求地址
                    dataType: "json", //返回的数据类型
                    //请求成功
                    success: function (data) {
                        if (data.code == 0) {
                            var cos = new COS({
                                getAuthorization: function (options, callback) {
                                    var credentials = data.data.credentials;
                                    callback({
                                        TmpSecretId: credentials.tmpSecretId,
                                        TmpSecretKey: credentials.tmpSecretKey,
                                        XCosSecurityToken: credentials.sessionToken,
                                        ExpiredTime: data.data.expiredTime
                                    });
                                }
                            });
                            cos.putObject({
                                Bucket: data.data.bucket,
                                Region: data.data.region,
                                Key: 'images/' + data.data.filename + choosefile.name,
                                Body: choosefile,
                            }, function (err, res) {
                                layer.closeAll('loading');
                                if (err) {
                                    layer.msg("上传失败", {icon: 5});
                                } else {
                                    if (res.statusCode == 200) {
                                        layer.msg('上传成功', {icon: 1, time: 2000});
                                        $("#app_bg_pic").val("https://" + res.Location);
                                    } else {
                                        layer.msg("图片上传失败", {icon: 5});
                                    }
                                }
                            });
                        } else {
                            layer.closeAll('loading');
                            layer.msg(data.msg, {icon: 5});
                        }
                    },
                    //请求失败
                    error: function (e) {
                        layer.closeAll('loading');
                        layer.msg('上传出现异常,请稍后再试', {icon: 2});
                    }
                });
            }
        });

        form.on('submit(formSubmit)', function(data){
            layer.confirm('确认要添加吗？', {icon: 6}, function () {
                $.ajax({
                    type: "POST", //请求方式
                    url: '/admin/rotary_table/add', //请求地址
                    dataType: "json", //返回的数据类型
                    data: $("#sbumitForm").serialize(), //数据
                    //请求成功
                    success: function (data) {
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 2000}, function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                window.parent.location.reload();
                            });
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 5000});
                        }
                    },
                    //请求失败
                    error: function (e) {
                        if (e.responseJSON.msg) {
                            layer.msg(e.responseJSON.msg, {icon: 2});
                        }
                    }
                });
            });
        });
    });

    function icon_view(id) {
        if (document.getElementById(id).value) {
            layer.photos({
                photos: {
                    "title": "",
                    "data": [{
                        "src": document.getElementById(id).value,
                    }]
                }
                , shade: 0.01
                , closeBtn: 1
            });
        }
    }

    function input_num(obj){
        obj.value = obj.value.replace(/[^\d.]/g,"");
        obj.value = obj.value.replace(/^\./g,"");
        obj.value = obj.value.replace(/\.{2,}/g,".");
        obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');

        if(obj.value.indexOf(".")> 0  && obj.value.indexOf("0")>2){
            obj.value= parseFloat(obj.value);
        }
        if(obj.value.indexOf(".")> 0  && obj.value.lastIndexOf("0")>2){
            obj.value= parseFloat(obj.value);
        }
        if (obj.value.indexOf(".") <= 0 && obj.value != "") {
            obj.value = parseFloat(obj.value);
        }
    }

</script>
</body>

</html>